package xiegd.com.springbootuse.demo.elaticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Author: xieguangda
 * @Date: 2021年08月09日 14:55
 */
/**
 * 类作用: Elasticsearch配置类
 * 类名称: ElasticsearchMonitorConfig
 * 类描述: Elasticsearch配置类
 */
@Configuration
public class ESConfig {
   // @Autowired
  //  private ElasticsearchMonitorProperties esProperties;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
       /*// String[] urlArr = esProperties.getEsAddress().split(",");
        String[] urlArr = "www.xieguangda.top:9200".split(",");
        HttpHost[] httpPostArr = new HttpHost[urlArr.length];
        for (int i = 0; i < urlArr.length; i++) {
            HttpHost httpHost = new HttpHost(urlArr[i].split(":")[0].trim(),
                    Integer.parseInt(urlArr[i].split(":")[1].trim()), "http");
            httpPostArr[i] = httpHost;
        }
        RestClientBuilder builder = RestClient.builder(httpPostArr);
        // 异步httpclient连接延时配置
        builder.setRequestConfigCallback(requestConfigBuilder -> {
//            requestConfigBuilder.setConnectTimeout(esProperties.getConnectTimeOut());
//            requestConfigBuilder.setSocketTimeout(esProperties.getSocketTimeOut());
//            requestConfigBuilder.setConnectionRequestTimeout(esProperties.getConnectionRequestTimeOut());

            requestConfigBuilder.setConnectTimeout(1000);
            requestConfigBuilder.setSocketTimeout(5000);
            requestConfigBuilder.setConnectionRequestTimeout(1000);

            HttpHost proxy = new HttpHost("127.0.0.1", 22, "http");
            requestConfigBuilder.setProxy(proxy);
            return requestConfigBuilder;
        });

        // 异步httpclient配置
        builder.setHttpClientConfigCallback(httpClientBuilder -> {
            // httpclient连接数配置
           // httpClientBuilder.setMaxConnTotal(esProperties.getMaxConnectNum());
           // httpClientBuilder.setMaxConnPerRoute(esProperties.getMaxConnectPerRoute());
            httpClientBuilder.setMaxConnTotal(30);
            httpClientBuilder.setMaxConnPerRoute(10);
            // httpclient保活策略
            return httpClientBuilder;
        });*/

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("www.xieguangda.top", 9200, "http"))
        );
        return client;
    }

}

